<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="./css/font.css">
    <link rel="stylesheet" href="./css/xadmin.css">
    <script type="text/javascript" src="./js/jquery.min.js"></script><script src="./js/common.js"></script>
    <script type="text/javascript" src="./lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="./js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="./js/html5.min.js"></script>
    <script src="./js/respond.min.js"></script>
    <![endif]-->
    <script>
        var baseUrl = parent.baseUrl;
        var env = $.getUrlParam("env");
        var gateway = $.getUrlParam("gateway");
        var likeDomain = $.getUrlParam("likeDomain");
        if (!likeDomain) {
            likeDomain = "";
        } else {
            likeDomain = encodeURI(likeDomain);
        }
        $(function () {
            $("#l_like_domain").val(decodeURI(decodeURI(likeDomain)));
        });
    </script>
</head>

<body class="layui-anim layui-anim-up">
<div class="x-nav">
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
</div>
<div class="x-body">
    <div class="layui-row">
        <form class="layui-form layui-col-md12 x-so">
            <input id="l_like_domain" type="text" name="likeDomain" placeholder="请输入域名" autocomplete="off"
                   class="layui-input">
            <button class="layui-btn" lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
        </form>
    </div>
    <xblock>
        <button class="layui-btn" onclick="openRouteDomainAdd()"><i class="layui-icon"></i>添加
        </button>
    </xblock>
    <table class="layui-table">
        <thead>
        <tr>
            <!--<th>网关</th>-->
            <th>域名</th>
            <th>路由个数</th>
            <th>备注</th>
            <th>创建时间</th>
            <th>状态</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody id="routeDomainListView">

        </tbody>
    </table>

</div>
<script>
    function openRouteDomainAdd() {
        x_admin_show('添加域名', './gateway-domain-add.html?env=' + env + "&gateway=" + gateway, 600, 400);
    }
</script>

<script>


    function openRouteDomainEditWin(id) {
        x_admin_show('编辑域名', './gateway-domain-edit.html?env=' + env + "&gateway=" + gateway + "&id=" + id, 600, 400);
    }

    /*-停用*/
    function routeDomain_stop(obj, id) {
        var msg = "可用";
        var newStatus = 1;

        if ($(obj).attr('title') == '可用') {
            msg = "不可用";
            newStatus = 0;
        }


        layer.confirm('确认要' + msg + '吗？', function (index) {

            var reqData = {
                request: {
                    status: newStatus, id: id
                }
            };

            $.ajax({
                url: baseUrl + "/servcieGateway/routeDomain/update?env=" + env + "&gateway=" + gateway,
                data: JSON.stringify(reqData),
                contentType: 'application/json;charset=utf-8',
                dataType: "JSON",
                async: false,
                type: "POST",
                success: function (respData) {
                    if (respData.respCode === '0000') {
                        if ($(obj).attr('title') == '可用') {

                            //发异步把用户状态进行更改
                            $(obj).attr('title', '不可用')
                            $(obj).find('i').html('&#xe62f;');

                            $(obj).parents("tr").find(".td-status").find('span').addClass('layui-btn-disabled').html('不可用');
                            layer.msg('不可用!', {icon: 4, time: 1000});

                        } else {
                            $(obj).attr('title', '可用')
                            $(obj).find('i').html('&#xe601;');

                            $(obj).parents("tr").find(".td-status").find('span').removeClass('layui-btn-disabled').addClass('layui-btn-normal').html('可用');
                            layer.msg('可用!', {icon: 6, time: 1000});
                        }
                    } else {
                        layer.msg(respData.respDesc, {
                            icon: 2,
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        }, function () {
                            //do something
                        });
                    }
                }
            });

        });
    }

    /*-删除*/
    function routeDomain_del(obj, id) {
        layer.confirm('确认要删除吗？', function (index) {
            $(function () {
                $.ajax({
                    url: baseUrl + "/servcieGateway/routeDomain/delete/" + id + "/?env=" + env + "&gateway=" + gateway,
                    data: "",
                    contentType: 'application/json;charset=utf-8',
                    dataType: "JSON",
                    async: false,
                    type: "GET",
                    success: function (respData) {
                        if (respData.respCode === '0000') {
                            //发异步删除数据
                            $(obj).parents("tr").remove();
                            layer.msg('已删除!', {icon: 1, time: 1000});

                        } else {
                            layer.msg(respData.respDesc, {
                                icon: 2,
                                time: 2000 //2秒关闭（如果不配置，默认是3秒）
                            }, function () {
                                //do something
                            });
                        }
                    }
                });

            });

        });
    }

</script>


<script>
    layui.use(['laytpl', 'layer'], function () {
        var laytpl = layui.laytpl;
        var layer = layui.layer;

        var likeDomain = $.getUrlParam("likeDomain");
        if (!likeDomain) {
            likeDomain = "";
        } else {
            likeDomain = encodeURI(likeDomain);
        }

        $.ajax({
            url: baseUrl + "/servcieGateway/routeDomain/list?likeDomain=" + likeDomain + "&env=" + env + "&gateway=" + gateway + "&applicationName=" + gateway,
            data: "",
            contentType: 'application/json;charset=utf-8',
            dataType: "JSON",
            // async: false,
            type: "GET",
            timeout: 10000,
            success: function (respData) {
                if (respData.respCode === '0000') {
                    var data = {
                        "list": respData.data.items
                    };

                    var getTpl = routeDomainListViewScript.innerHTML
                        , view = document.getElementById('routeDomainListView');
                    laytpl(getTpl).render(data, function (html) {
                        view.innerHTML = html;
                    });
                } else {
                    layer.msg(respData.respDesc, {
                        icon: 2,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    }, function () {
                        //do something
                    });
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                if (textStatus == "timeout") {
                    var inx = layer.alert("加载超时，服务器正常的情况下，请检查数据源配置是否正确!", {icon: 2}, function () {
                        layer.close(inx);
                    });
                } else {
                    // alert(textStatus);
                }
            }
        });
    });
</script>

<script id="routeDomainListViewScript" type="text/html">
    {{# layui.each(d.list, function(index, item){ }}

    <tr>
        <td hidden> {{ item.id }}</td>
        <!--<td>{{ item.applicationName }}</td>-->
        <td>{{ item.domain }}</td>
        <td>{{ item.routes.length }}</td>
        <td>{{ item.comment }}</td>
        <td>{{ item.createTime }}</td>

        {{# if(item.statusCode === 0){ }}
        <td class="td-status">
            <span class="layui-btn layui-btn-disabled layui-btn-sm">{{ item.statusDesc }}</span>
        </td>
        {{# } }}


        {{# if(item.statusCode === 1){ }}
        <td class="td-status">
            <span class="layui-btn layui-btn-normal layui-btn-sm">{{ item.statusDesc }}</span>
        </td>
        {{# } }}


        <td class="td-manage">

            {{# if(item.statusCode === 1){ }}
            <a onclick="routeDomain_stop(this,'{{ item.id }}')" href="javascript:;" title="可用">
                <i class="layui-icon">&#xe601;</i>
            </a>
            {{# } }}

            {{# if(item.statusCode === 0){ }}
            <a onclick="routeDomain_stop(this,'{{ item.id }}')" href="javascript:;" title="不可用">
                <i class="layui-icon">&#xe62f;</i>
            </a>
            {{# } }}


            <a title="编辑" onclick="openRouteDomainEditWin('{{ item.id }}')" href="javascript:;">
                <i class="layui-icon">&#xe642;</i>
            </a>
            <a title="删除" onclick="routeDomain_del(this,'{{ item.id }}')" href="javascript:;">
                <i class="layui-icon">&#xe640;</i>
            </a>
        </td>
    </tr>

    {{# }); }}
</script>


<script>
    layui.use(['form', 'layer'], function () {
        $ = layui.jquery;
        var form = layui.form
            , layer = layui.layer;

        //监听提交
        form.on('submit(sreach)', function (data) {
            var likeDomain = $.trim(data.field.likeDomain);
            if (likeDomain) {
                likeDomain = encodeURI(encodeURI(likeDomain));
            }

            var url = "./gateway-domain-list.html?likeDomain=" + likeDomain + "&env=" + env + "&gateway=" + gateway;
            window.location.href = url;
            return false;
        });

    });
</script>

<script>var _hmt = _hmt || [];
(function () {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
})();</script>
</body>

</html>